package com.quick.controller.system;

import java.util.List;

import com.quick.system.domain.SysDept;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.system.domain.SysUser;
import com.quick.system.service.SysUserService;
import com.quick.common.utils.poi.ExcelUtil;

/**
 * 用户信息Controller
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@Api(value = "用户信息管理", tags = {"用户信息操作接口"})
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ps.hasPermission('system:user:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询用户信息列表")
    public ApiResponse list(SysUser sysUser) {
        try {
            startPage();
            List<SysUser> list = sysUserService.selectSysUserList(sysUser);
            return paging(list);
        } finally {
            clearPage();
        }
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ps.hasPermission('system:user:export')")
    @Log(title = "用户信息", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出用户信息列表")
    public void export(HttpServletResponse response, SysUser sysUser) {
        List<SysUser> list = sysUserService.selectSysUserList(sysUser);
        ExcelUtil<SysUser> util = new ExcelUtil<>(SysUser.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ps.hasPermission('system:user:query')")
    @GetMapping(value = "/{userId}")
    @ApiOperation(value = "获取用户信息详细信息")
    public ApiResponse getInfo(@PathVariable(value = "userId") Long userId) {
        return success(sysUserService.getInfo(userId));
    }


    /**
     * 获取职位和角色
     */
    @PreAuthorize("@ps.hasPermission('system:user:postAndRole')")
    @GetMapping(value = "/postAndRole")
    @ApiOperation(value = "获取职位和角色")
    public ApiResponse getPostAndRole() {
        return success(sysUserService.getPostAndRole());
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ps.hasPermission('system:user:add')")
    @Log(title = "用户信息", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation(value = "新增用户信息")
    public ApiResponse add(@RequestBody SysUser sysUser) {
        return success(sysUserService.insertSysUser(sysUser));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ps.hasPermission('system:user:edit')")
    @Log(title = "用户信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改用户信息")
    public ApiResponse edit(@RequestBody SysUser sysUser) {
        return success(sysUserService.updateSysUser(sysUser));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ps.hasPermission('system:user:remove')")
    @Log(title = "用户信息", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{userIds}")
    @ApiOperation(value = "删除用户信息")
    public ApiResponse remove(@PathVariable Long[] userIds) {
        return success(sysUserService.deleteSysUserByUserIds(userIds));
    }


    /**
     * 重置密码
     */
    @PreAuthorize("@ps.hasPermission('system:user:resetPwd')")
    @Log(title = "用户信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/resetPwd")
    public ApiResponse resetPwd(@RequestBody SysUser user) {
        return success(sysUserService.resetPwd(user));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ps.hasPermission('system:user:changeStatus')")
    @Log(title = "用户信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping("/changeStatus")
    public ApiResponse changeStatus(@RequestBody SysUser user) {
        return success(sysUserService.changeStatus(user));
    }


    /**
     * 根据用户编号获取授权角色
     */
    @PreAuthorize("@ps.hasPermission('system:user:query')")
    @GetMapping("/authRole/{userId}")
    public ApiResponse authRole(@PathVariable("userId") Long userId) {
        return success(sysUserService.authRole(userId));
    }

    /**
     * 用户授权角色
     */
    @PreAuthorize("@ps.hasPermission('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessTypeEnum.GRANT)
    @PutMapping("/authRole")
    public ApiResponse insertAuthRole(Long userId, Long[] roleIds) {
        sysUserService.insertUserAuth(userId, roleIds);
        return success();
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ps.hasPermission('system:user:list')")
    @GetMapping("/deptTree")
    public ApiResponse deptTree(SysDept dept) {
        return success();
    }
}
